package org.htmlparser;

import java.io.PrintStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.Hashtable;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.http.ConnectionManager;
import org.htmlparser.http.ConnectionMonitor;
import org.htmlparser.http.HttpHeader;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.DefaultParserFeedback;
import org.htmlparser.util.IteratorImpl;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.ParserFeedback;
import org.htmlparser.visitors.NodeVisitor;

public class Parser
  implements Serializable, ConnectionMonitor
{
  public static final ParserFeedback DEVNULL = new DefaultParserFeedback(0);
  public static final ParserFeedback STDOUT = new DefaultParserFeedback();
  public static final String VERSION_DATE = "Jun 10, 2006";
  public static final double VERSION_NUMBER = 1.6000000000000001D;
  public static final String VERSION_STRING = "1.6 (Release Build Jun 10, 2006)";
  public static final String VERSION_TYPE = "Release Build";
  protected ParserFeedback mFeedback;
  protected Lexer mLexer;

  static
  {
    getConnectionManager();
    ConnectionManager.getDefaultRequestProperties().put("User-Agent", "HTMLParser/" + getVersionNumber());
  }

  public Parser()
  {
    this(new Lexer(new Page("")), DEVNULL);
  }

  public Parser(String paramString)
    throws ParserException
  {
    this(paramString, STDOUT);
  }

  public Parser(String paramString, ParserFeedback paramParserFeedback)
    throws ParserException
  {
    setFeedback(paramParserFeedback);
    setResource(paramString);
    setNodeFactory(new PrototypicalNodeFactory());
  }

  public Parser(URLConnection paramURLConnection)
    throws ParserException
  {
    this(paramURLConnection, STDOUT);
  }

  public Parser(URLConnection paramURLConnection, ParserFeedback paramParserFeedback)
    throws ParserException
  {
    this(new Lexer(paramURLConnection), paramParserFeedback);
  }

  public Parser(Lexer paramLexer)
  {
    this(paramLexer, STDOUT);
  }

  public Parser(Lexer paramLexer, ParserFeedback paramParserFeedback)
  {
    setFeedback(paramParserFeedback);
    setLexer(paramLexer);
    setNodeFactory(new PrototypicalNodeFactory());
  }

  public static Parser createParser(String paramString1, String paramString2)
  {
    if (paramString1 == null)
      throw new IllegalArgumentException("html cannot be null");
    return new Parser(new Lexer(new Page(paramString1, paramString2)));
  }

  public static ConnectionManager getConnectionManager()
  {
    return Page.getConnectionManager();
  }

  public static String getVersion()
  {
    return "1.6 (Release Build Jun 10, 2006)";
  }

  public static double getVersionNumber()
  {
    return 1.6000000000000001D;
  }

  public static void main(String[] paramArrayOfString)
  {
    Parser localParser;
    TagNameFilter localTagNameFilter;
    if ((paramArrayOfString.length < 1) || (paramArrayOfString[0].equals("-help")))
    {
      System.out.println("HTML Parser v" + getVersion() + "\n");
      System.out.println();
      System.out.println("Syntax : java -jar htmlparser.jar <file/page> [type]");
      System.out.println("   <file/page> the URL or file to be parsed");
      System.out.println("   type the node type, for example:");
      System.out.println("     A - Show only the link tags");
      System.out.println("     IMG - Show only the image tags");
      System.out.println("     TITLE - Show only the title tag");
      System.out.println();
      System.out.println("Example : java -jar htmlparser.jar http://www.yahoo.com");
      System.out.println();
      return;
    }
    try
    {
      localParser = new Parser();
      if (1 < paramArrayOfString.length)
      {
        localTagNameFilter = new TagNameFilter(paramArrayOfString[1]);
        getConnectionManager().setRedirectionProcessingEnabled(true);
        getConnectionManager().setCookieProcessingEnabled(true);
        localParser.setResource(paramArrayOfString[0]);
        System.out.println(localParser.parse(localTagNameFilter));
      }
    }
    catch (ParserException localParserException)
    {
      while (true)
      {
        while (true)
          localParserException.printStackTrace();
        localTagNameFilter = null;
        localParser.setFeedback(STDOUT);
        getConnectionManager().setMonitor(localParser);
      }
    }
  }

  public static void setConnectionManager(ConnectionManager paramConnectionManager)
  {
    Page.setConnectionManager(paramConnectionManager);
  }

  public NodeIterator elements()
    throws ParserException
  {
    return new IteratorImpl(getLexer(), getFeedback());
  }

  public NodeList extractAllNodesThatMatch(NodeFilter paramNodeFilter)
    throws ParserException
  {
    NodeList localNodeList = new NodeList();
    NodeIterator localNodeIterator = elements();
    while (localNodeIterator.hasMoreNodes())
      localNodeIterator.nextNode().collectInto(localNodeList, paramNodeFilter);
    return localNodeList;
  }

  public URLConnection getConnection()
  {
    return getLexer().getPage().getConnection();
  }

  public String getEncoding()
  {
    return getLexer().getPage().getEncoding();
  }

  public ParserFeedback getFeedback()
  {
    return this.mFeedback;
  }

  public Lexer getLexer()
  {
    return this.mLexer;
  }

  public NodeFactory getNodeFactory()
  {
    return getLexer().getNodeFactory();
  }

  public String getURL()
  {
    return getLexer().getPage().getUrl();
  }

  public NodeList parse(NodeFilter paramNodeFilter)
    throws ParserException
  {
    NodeList localNodeList = new NodeList();
    NodeIterator localNodeIterator = elements();
    while (true)
    {
      Node localNode;
      while (true)
      {
        if (!(localNodeIterator.hasMoreNodes()))
          break label55;
        localNode = localNodeIterator.nextNode();
        if (paramNodeFilter == null)
          break;
        localNode.collectInto(localNodeList, paramNodeFilter);
      }
      localNodeList.add(localNode);
    }
    label55: return localNodeList;
  }

  public void postConnect(HttpURLConnection paramHttpURLConnection)
    throws ParserException
  {
    getFeedback().info(HttpHeader.getResponseHeader(paramHttpURLConnection));
  }

  public void preConnect(HttpURLConnection paramHttpURLConnection)
    throws ParserException
  {
    getFeedback().info(HttpHeader.getRequestHeader(paramHttpURLConnection));
  }

  public void reset()
  {
    getLexer().reset();
  }

  public void setConnection(URLConnection paramURLConnection)
    throws ParserException
  {
    if (paramURLConnection == null)
      throw new IllegalArgumentException("connection cannot be null");
    setLexer(new Lexer(paramURLConnection));
  }

  public void setEncoding(String paramString)
    throws ParserException
  {
    getLexer().getPage().setEncoding(paramString);
  }

  public void setFeedback(ParserFeedback paramParserFeedback)
  {
    if (paramParserFeedback == null)
      this.mFeedback = DEVNULL;
    while (true)
    {
      return;
      this.mFeedback = paramParserFeedback;
    }
  }

  public void setInputHTML(String paramString)
    throws ParserException
  {
    if (paramString == null)
      throw new IllegalArgumentException("html cannot be null");
    if (!("".equals(paramString)))
      setLexer(new Lexer(new Page(paramString)));
  }

  public void setLexer(Lexer paramLexer)
  {
    if (paramLexer == null)
      throw new IllegalArgumentException("lexer cannot be null");
    NodeFactory localNodeFactory = null;
    if (getLexer() != null)
      localNodeFactory = getLexer().getNodeFactory();
    if (localNodeFactory != null)
      paramLexer.setNodeFactory(localNodeFactory);
    this.mLexer = paramLexer;
    String str = this.mLexer.getPage().getContentType();
    if ((str != null) && (!(str.startsWith("text"))))
      getFeedback().warning("URL " + this.mLexer.getPage().getUrl() + " does not contain text");
  }

  public void setNodeFactory(NodeFactory paramNodeFactory)
  {
    if (paramNodeFactory == null)
      throw new IllegalArgumentException("node factory cannot be null");
    getLexer().setNodeFactory(paramNodeFactory);
  }

  public void setResource(String paramString)
    throws ParserException
  {
    if (paramString == null)
      throw new IllegalArgumentException("resource cannot be null");
    int i = paramString.length();
    int j = 0;
    int k = 0;
    if (k < i)
    {
      char c = paramString.charAt(k);
      if (Character.isWhitespace(c))
        break label80;
      if ('<' == c)
        j = 1;
    }
    if (j != 0)
      setLexer(new Lexer(new Page(paramString)));
    while (true)
    {
      while (true)
      {
        return;
        label80: ++k;
      }
      setLexer(new Lexer(getConnectionManager().openConnection(paramString)));
    }
  }

  public void setURL(String paramString)
    throws ParserException
  {
    if ((paramString != null) && (!("".equals(paramString))))
      setConnection(getConnectionManager().openConnection(paramString));
  }

  public void visitAllNodesWith(NodeVisitor paramNodeVisitor)
    throws ParserException
  {
    paramNodeVisitor.beginParsing();
    NodeIterator localNodeIterator = elements();
    while (localNodeIterator.hasMoreNodes())
      localNodeIterator.nextNode().accept(paramNodeVisitor);
    paramNodeVisitor.finishedParsing();
  }
}